package com.start.boot.service.impl;


import com.start.boot.domain.ChartNOGroup;
import com.start.boot.domain.ChartWithGroup;
import com.start.boot.dao.ajpc.QueryMapper;
import com.start.boot.dao.ajpc.YX_PC_JBXXMapper;
import com.start.boot.domain.Tongji;
import com.start.boot.pojo.dto.*;
import com.start.boot.pojo.vo.CountVo;
import com.start.boot.pojo.vo.PcxVo;
import com.start.boot.query.Query;
import com.start.boot.service.CountService;
import com.start.boot.support.utils.DataAccessHelper;
import com.start.boot.support.utils.EasyUIHelper;
import com.start.boot.utils.QueryUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class CountServiceImpl implements CountService {

    @Autowired
    QueryMapper queryMapper;


    @Autowired
    QueryUtils queryUtils;


    @Autowired
    YX_PC_JBXXMapper jbxxMapper;

    @Override
    public Map<Integer, List<CountDo>> getPcByYear(Integer year, String pcflbm) {
        List<CountDo> query = queryMapper.query(year,pcflbm);
        if (query.isEmpty()){
            return null;
        }
        Map<Integer, List<CountDo>> collect = query.stream()
                .collect(Collectors.groupingBy(CountDo::getMonth, Collectors.toList()));
        List<MonthDo> monthDos = queryMapper.querySum(year,pcflbm);
        Set<Map.Entry<Integer, List<CountDo>>> entries = collect.entrySet();
        if (entries!=null){
            entries.stream().forEach((Map.Entry<Integer, List<CountDo>> data) ->{
                List<CountDo> value = data.getValue();
                if(value!=null){
                    value.stream().forEach(month->{
                        monthDos.stream().forEach(sum->{
                            if (month.getMonth()==sum.getMonth()){
                                month.setPercent(deciMal(month.getCount(),sum.getSum()));
                            }
                        });

                    });
                }

            });
        }

       return collect;
    }

    private double deciMal(int top, int below) {
        double result = new BigDecimal((float)top / below).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        return result*100;
    }


    @Override
    public List<CountVo> getYearPercentByYearAndPcflbm(Integer year, String pcflbm) {
        Integer sum = queryMapper.getYearCountByYearAndPcflbm(year, pcflbm);
        List<CountDo> yearPcjgCount = queryMapper.getYearPcjgCount(year, pcflbm);
        if (!yearPcjgCount.isEmpty()){
            ArrayList<CountVo> result = new ArrayList<>();
            yearPcjgCount.stream().forEach(data->{
                CountVo countVo = new CountVo();
                data.setPercent(deciMal(data.getCount(),sum));
                countVo.setPcjg(data.getPcjg()==null?"其它":data.getPcjg());
                countVo.setValue(data);
                result.add(countVo);
            });
            return result;
        }
        return null;
    }

    @Override
    public List<PcxVo> getPcxFlByYearAndMbbmAndPcjg(String pcflbm, Integer year) throws Exception {
        ArrayList<PcxVo> VoResult = new ArrayList<>();
        Query query = new Query();
        query.setClazz(PcMbDto.class)
                .setTableName("xt_pc_mb").createCriteria()
                .andEqualTo("PCFLBM",pcflbm)
                .andEqualTo("SFQY","Y")
                ;
        List<PcMbDto> javaBean = queryUtils.getJavaBean(query.build());
        if (javaBean.isEmpty()){
            return null;
        }
        String pcmbbm = javaBean.get(0).getPcmbbm();

        Query queryPcflbm = new Query();
        queryPcflbm.setTableName("xt_pc_pcxfl")
                    .createCriteria()
                    .andEqualTo("PCMBBM",pcmbbm)
                    .andIsNotNull("PCXFLFBM");


        List<Map> result = queryUtils.getMap(queryPcflbm);

        HashMap<String, ArrayList<String>> temp = new HashMap<>();

        result.stream().forEach(data->{
            String pcxflmc = (String) data.get("PCXFLMC");
            ArrayList<String> value = temp.getOrDefault(pcxflmc, null);
            String pcxflbm = (String) data.get("PCXFLBM");
            if (value==null){
                ArrayList<String> strings = new ArrayList<>();
                strings.add(pcxflbm);
                temp.put(pcxflmc,strings);
            }else {
                value.add(pcxflbm);
            }
        });

        Set<Map.Entry<String, ArrayList<String>>> entries = temp.entrySet();
        if (entries.isEmpty()){
            return null;
        }
        //分月数据处理
        HashMap<Integer,List<String>>pcslbmMap=new HashMap<>();
        //根据年份查评查受理编码list
        List<JbxxDto> pcslbmListByYearAndMonth = queryMapper.getPcslbmListByYearAndMonth(year);
        if (!pcslbmListByYearAndMonth.isEmpty()){
            pcslbmListByYearAndMonth.stream().forEach(data->{
                Integer month = data.getMonth();
                List<String> pcslbmList = pcslbmMap.getOrDefault(month, null);
                String pcxflbm =  data.getPcslbm();
                if (pcslbmList==null){
                    ArrayList<String> pcslbmListTemp = new ArrayList<>();
                    pcslbmListTemp.add(pcxflbm);
                    pcslbmMap.put(month,pcslbmListTemp);
                }else {
                    pcslbmList.add(pcxflbm);
                }
            });
        }
        Set<Map.Entry<Integer, List<String>>> pcslbmEntry = pcslbmMap.entrySet();
        entries.stream().forEach(data->{
            String key = data.getKey();
            ArrayList<String> pcxbmList = data.getValue();
            if (!data.getValue().isEmpty()){
                pcslbmEntry.forEach(pcslbmList->{
                    Query pcxDtoQuery = new Query();
                    LinkedList<String> groupByList = new LinkedList<>();
                    groupByList.add("PCXMC");
                    HashMap<String, String> displayName = new HashMap<>();
                    displayName.put("COUNT(*)","count");
                    displayName.put("PCXMC",null);
                    pcxDtoQuery.setTableName("YX_PC_PCX")
                            .setClazz(PcxDto.class)
                            .setDisplayColumnName(displayName)
                            .setGroupByClause(groupByList)
                            .setOrderByClause("1 DESC")
                            .createCriteria()
                            .andin("PCXBM",pcxbmList)
                            .andin("PCSLBM",pcslbmList.getValue())
                            .andNotEqualTo("PCJG","0")
                    ;
                    try {//只返回前5个
                        List<PcxDto> dtoList = queryUtils.getJavaBean(pcxDtoQuery);
                        PcxVo pcxVo = new PcxVo();
                        pcxVo.setName(key);
                        pcxVo.setMonth(pcslbmList.getKey());
                        int n=5;
                        if (dtoList!=null){
                            if (dtoList.size()<5){
                                n=dtoList.size();
                            }
                            pcxVo.setResult(dtoList.subList(0,n));
                            VoResult.add(pcxVo);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                });

            }
        });
        return VoResult;
    }

    @Override
    public List<PcxVo> getPcxFlByYearAndBinTu(String pcflbm, Integer year,String pcjg) throws Exception {
        ArrayList<PcxVo> VoResult = new ArrayList<>();
        Query query = new Query();
        query.setClazz(PcMbDto.class)
                .setTableName("xt_pc_mb").createCriteria()
                .andEqualTo("PCFLBM",pcflbm)
                .andEqualTo("SFQY","Y")
        ;
        List<PcMbDto> javaBean = queryUtils.getJavaBean(query.build());
        if (javaBean.isEmpty()){
            return null;
        }
        String pcmbbm = javaBean.get(0).getPcmbbm();

        Query queryPcflbm = new Query();
        queryPcflbm.setTableName("xt_pc_pcxfl")
                .createCriteria()
                .andEqualTo("PCMBBM",pcmbbm)
                .andlike("PCXFLMC",pcjg)
                .andIsNotNull("PCXFLFBM");


        List<Map> result = queryUtils.getMap(queryPcflbm);

        HashMap<String, ArrayList<String>> temp = new HashMap<>();

        result.stream().forEach(data->{
            String pcxflmc = (String) data.get("PCXFLMC");
            ArrayList<String> value = temp.getOrDefault(pcxflmc, null);
            String pcxflbm = (String) data.get("PCXFLBM");
            if (value==null){
                ArrayList<String> strings = new ArrayList<>();
                strings.add(pcxflbm);
                temp.put(pcxflmc,strings);
            }else {
                value.add(pcxflbm);
            }
        });

        Set<Map.Entry<String, ArrayList<String>>> entries = temp.entrySet();
        if (entries.isEmpty()){
            return null;
        }
        entries.stream().forEach(data->{
            ArrayList<String> pcxbmList = data.getValue();
            //根据年份查评查受理编码list
            List<String> pcslbmList = queryMapper.getPcslbmListByYear(year);

            Query pcxDtoQuery = new Query();
            LinkedList<String> groupByList = new LinkedList<>();
            groupByList.add("PCXMC");
            HashMap<String, String> displayName = new HashMap<>();
            displayName.put("COUNT(*)","count");
            displayName.put("PCXMC",null);
            pcxDtoQuery.setTableName("YX_PC_PCX")
                    .setClazz(PcxDto.class)
                    .setDisplayColumnName(displayName)
                    .setGroupByClause(groupByList)
                    .setOrderByClause("1 DESC")
                    .createCriteria()
                    .andin("PCXBM",pcxbmList)
                    .andin("PCSLBM",pcslbmList)
                    .andNotEqualTo("PCJG","0");
            try {
                List<PcxDto> dtoList = queryUtils.getJavaBean(pcxDtoQuery);
                PcxVo pcxVo = new PcxVo();
                pcxVo.setName(data.getKey());
                pcxVo.setResult(dtoList);
                VoResult.add(pcxVo);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        return VoResult;
    }


    @Override
    public List<ChartWithGroup> getAjzlLinesByYearAndPcflbm(Integer year,String pcflbm){
        List<ChartWithGroup> linesByYearAndPcflbm = queryMapper.getAjzlLinesByYearAndPcflbm(year,pcflbm);
        return linesByYearAndPcflbm;
    }

    @Override
    public List<ChartNOGroup> getAjzlPieByYearAndPcflbm(Integer year, String pcflbm) throws Exception {
        List<ChartNOGroup> pieByYearAndPcflbm = queryMapper.getAjzlPieByYearAndPcflbm(year, pcflbm);
        return pieByYearAndPcflbm;
    }

    @Override
    public List<Map> getJbpctotal(Tongji tongji) {

        Map map = new HashMap();
        map.put("p_dwbm",tongji.getDwbm());
        map.put("p_bmbm",tongji.getBmbm());
        map.put("p_gh",tongji.getGh());
        map.put("p_pagesize",tongji.getRows());
        map.put("p_pageindex",tongji.getPage());

        queryMapper.getJbpctotal(map);

        return DataAccessHelper.getListMap(map,"p_cursor");
    }


}